package org.cassandra;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class CassandraSync {

    public static void main(String[] args) {
        // Cassandra JDBC连接URL
        String jdbcUrl = "jdbc:cassandra://ld-uf68jbw542qiiw534-proxy-lindorm-pub.lindorm.rds.aliyuncs.com:9042/tyiotcloud";

        // Cassandra数据库用户名和密码（如果启用了验证）
        String username = "root";
        String password = "dr0VqJuy";

        Connection connection = null;

        try {
            // 注册Cassandra JDBC驱动
            Class.forName("org.apache.cassandra.cql.jdbc.CassandraDriver");

            // 建立连接
            if (username != null && password != null) {
                connection = DriverManager.getConnection(jdbcUrl, username, password);
            } else {
                connection = DriverManager.getConnection(jdbcUrl);
            }

            // 执行查询
            String query = "SELECT entity_type,entity_id,key,bool_v,dbl_v,json_v,long_v,str_v,ts FROM ts_kv_latest_cf where ts >= 1674323507000  ALLOW FILTERING";
            PreparedStatement preparedStatement = connection.prepareStatement(query);
            ResultSet resultSet = preparedStatement.executeQuery();

            // 处理查询结果
            while (resultSet.next()) {
                String column1Value = resultSet.getString("column1");
                String column2Value = resultSet.getString("column2");

                System.out.println("Column1: " + column1Value + ", Column2: " + column2Value);
            }
        } catch (ClassNotFoundException | SQLException e) {
            e.printStackTrace();
        } finally {
            // 关闭连接
            if (connection != null) {
                try {
                    connection.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }

}
